<?php
/**
 * 活动好友
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019-03-28
 * Time: 下午 11:26
 */
namespace App;


use Model\ActivityMemberMod;
use Model\ActivitySystemFriendMod;
use Model\FriendAwardRecordMod;
use Model\InviteMod;

class Friend extends Action
{
    private $ActivitySystemFriend = null;  //活动系统好友
    private $FriendAwardRecord = null;  //好友奖品领取记录
    private $ActivityMember = null;
    private $Invite = null;   //好友（邀请记录）

	public function __construct()
	{
		parent::__construct();
	}

    /**
     * 获取指定用户的好友列表
     * @param uid 用户id
     * @param activity_id 活动id
     * @param limit 请求下标[0,10]
     */
	public function getList()
    {
        $this->Invite = new InviteMod();
        $_list = $this->Invite->uidToFriendList($this->_gpc['uid'],$this->_gpc['activity_id'],$this->_gpc['limit']);
        $this->FriendAwardRecord = new FriendAwardRecordMod();
        if(count($_list) > 0){
            $_data = array();
            foreach ($_list as $_k=>$_v){
                $_return_uid = 0;
                if($this->_gpc["uid"] == $_v["share_uid"]){
                    $_return_uid = $_v["invite_uid"];
                }else{
                    $_return_uid = $_v["share_uid"];
                }
                $_member = $this->uidToInfo($_return_uid);
                $_may_award = $this->FriendAwardRecord->findOne(array('friend_id'=>$_v['id'],'uid'=>$this->_gpc['uid'],'from'=>1),array('id')) ? 0 : 1;
                $_data[] = array("id"=>$_v["id"],"name"=>$_member["nickname"],"header"=>$_member["avatar"],"may_award"=>$_may_award,"open_count"=>$_v["open_count"]);
            }
            unset($_list);
            $this->_result = array("status"=>"success","data"=>$_data);
        }
        $this->result($this->_result);
    }

    /**
     * 获取好友总数
     * @param uid 用户id
     * @param activity_id 活动id
     */
    public function getTotal()
    {
        $_total = 0;
        $this->ActivitySystemFriend = new ActivitySystemFriendMod();
        $_total += $this->ActivitySystemFriend->total(array("activity_id"=>$this->_gpc["activity_id"]));
        $this->Invite = new InviteMod();
        $_total += $this->Invite->uidToFriendTotal($this->_gpc["uid"],$this->_gpc["activity_id"]);
        $this->_result = array("status"=>"success","data"=>$_total);
        $this->result($this->_result);
    }

    /**
     * 领取好友奖励
     * @param friend_id 好友关系id
     * @param number 奖励数量
     * @param from 来自哪里  1=好友，2=系统好友
     * @param uid 用户id
     * @param activity_id 活动id
     */
    public function getAward()
    {
        $this->FriendAwardRecord = new FriendAwardRecordMod();
        $this->FriendAwardRecord->insert(array(
            'friend_id'=>$this->_gpc['friend_id'],
            'uid'=>$this->_gpc['uid'],
            'from'=>$this->_gpc['from'],
            'number'=>$this->_gpc['number']
        ));
        $this->ActivityMember = new ActivityMemberMod();
        $_result = $this->ActivityMember->math(
            array('open_num +'=>$this->_gpc['number']),
            array('uid'=>$this->_gpc['uid'],'activity_id'=>$this->_gpc['activity_id'])
        );
        if($_result){
            $this->_result = array('status'=>'success');
        }
        $this->result($this->_result);
    }


    /**
     * --------------系统好友分割线-----------
     */

    /**
     * 添加系统好友
     * @param data 数据
     */
    public function addSystemFriend()
    {
        $this->ActivitySystemFriend = new ActivitySystemFriendMod();
        if($this->ActivitySystemFriend->insert($this->_gpc['data'])){
            $this->_result = array('status'=>'success');
        }
        $this->result($this->_result);
    }

    /**
     * 获取活动系统好友列表
     * @param activity_id 活动id
     */
    public function getSystemFriendList()
    {
        $this->ActivitySystemFriend = new ActivitySystemFriendMod();
        $_list = $this->ActivitySystemFriend->findAll(array('activity_id'=>$this->_gpc['activity_id']),array('order'=>array('id','asc')),array('id','name','header','open_count'));
        if(count($_list) > 0){
            foreach($_list as $_k=>$_v){
                $_list[$_k]["header"] = $_v["header"];
            }
            $this->_result = array('status'=>'success','data'=>$_list);
        }
        $this->result($this->_result);
    }

    /**
     * 获取活动系统好友列表
     * @param activity_id 活动id
     * @param uid 用户id
     */
    public function getFrontSystemFriendList()
    {
        $this->ActivitySystemFriend = new ActivitySystemFriendMod();
        $_list = $this->ActivitySystemFriend->findAll(array('activity_id'=>$this->_gpc['activity_id']),array('order'=>array('id','asc')),array('id','name','header','open_count'));
        if(count($_list) > 0){
            $this->FriendAwardRecord = new FriendAwardRecordMod();
            foreach($_list as $_k=>$_v){
                $_list[$_k]["header"] = tomedia($_v["header"]);
                $_list[$_k]['may_award'] = $this->FriendAwardRecord->findOne(array('friend_id'=>$_v['id'],'uid'=>$this->_gpc['uid'],'from'=>2),array('id')) ? 0 : 1;
            }
            $this->_result = array('status'=>'success','data'=>$_list);
        }
        $this->result($this->_result);
    }

    /**
     * 删除系统好友
     * @param friend_id
     */
    public function deleteSystemFriend()
    {
        $this->ActivitySystemFriend = new ActivitySystemFriendMod();
        if($this->ActivitySystemFriend->delete(array('id'=>$this->_gpc['friend_id']))){
            $this->_result = array("status"=>"success");
        }
        $this->result($this->_result);
    }

}